<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .router-link-exact-active {
      color: red;
    }
  </style>
</head>
<body>
  <div id="app">
    <div>
      <!-- <router-link to="/">全部</router-link>
      <router-link to="/good">精华</router-link>
      <router-link to="/share">分享</router-link>
      <router-link to="/ask">问答</router-link>
      <router-link to="/job">招聘</router-link>
      <router-link to="/dev">测试</router-link> -->
      <router-link to="/">全部</router-link>
      <router-link to="/?tab=good">精华</router-link>
      <router-link to="/?tab=share">分享</router-link>
      <router-link to="/?tab=ask">问答</router-link>
      <router-link to="/?tab=job">招聘</router-link>
      <router-link to="/?tab=dev">测试</router-link>
    </div>
    <router-view></router-view>
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <script src="./axios.min.js"></script>
  <script>
    const nodeList = {
      template: `
        <ul>
          <li v-for="topic in topics">{{topic.title}}</li>
        </ul>
      `,
      data () {
        return {
          topics: []
        }
      },
      watch: {
        /* '$route': {
          handler (to) {
            const tab = to.params.tab
            console.log(tab)
            this.topics = []
            axios.get('https://cnodejs.org/api/v1/topics', {
              params: {
                // tab: tab
                tab
              }
            }).then(res => {
              this.topics = res.data.data
            })
          },
          immediate: true
        } */
        '$route': {
          handler (to) {
            const tab = to.query.tab
            this.topics = []
            axios.get('https://cnodejs.org/api/v1/topics', {
              params: {
                tab
              }
            }).then(res => {
              this.topics = res.data.data
            })
          },
          immediate: true
        }
      }
    }
    
    const routes = [
      /* {
        // 加问号和不加问号的区别，能够识别多一个路由 /
        path: '/:tab?',
        component: nodeList
      } */
      {
        path: '/',
        component: nodeList
      }
    ]

    const router = new VueRouter({
      routes
    })

    const app = new Vue({
      el: "#app",
      router
    })
  </script>
</body>
</html>